package com.dg11185.nearshop.db.entity;

import java.sql.SQLException;
import java.util.List;

import com.dg11185.libs.ormlite.dao.Dao;
import com.dg11185.libs.ormlite.stmt.UpdateBuilder;
import com.dg11185.libs.utils.date.DateUtils;
import com.dg11185.libs.utils.netstate.NetWorkUtils;

/**
 * 数据库所用到的相关常量字段
 * 
 * @author：Daibo
 * @time：2014-5-20 下午3:27:30
 */
public class DbConstant {

    /** 数据库表名 */
    // **{@link LastUpdateTime}
    public final static String LAST_UPDATE_TIME = "NEARSHOP_LAST_UPDATE_TIME";

    // {@link Area}
    public final static String AREA = "NEARSHOP_AREA";

    // {@link Industry}
    public final static String INDUSTRY = "NEARSHOP_INDUSTRY";

    // {@link Location}
    public final static String LOCATION = "NEARSHOP_LOCATION";

    // {@link MerchantInfo}
    public final static String MERCHANT = "NEARSHOP_MERCHANT";
    // {@link Tabloid}
    public final static String TABLOID = "NEARSHOP_TABLOID";

    /** {@link LastUpdateTime}记录各个表的最后更新时间，以下字段记录每个表多长时间更新一次 */
    // 用户所在城市商圈数据过期时间(分钟)10*24*60
    public final static long AREA_UPDATE_TIME = 10 * 24 * 60;

    // 商家类型数据过期时间(分钟)
    public final static long INDUSTRY_UPDATE_TIME = 10 * 24 * 60;

    // 定位类型数据过期时间(分钟)
    public final static long LOCATION_UPDATE_TIME = 30;

    /**
     * 
     * 删除原来的表中数据，插入新数据并更新TimeTable
     * 
     * @param <T>
     * @param tableName
     *            作为LastUpdateTime表的一个字段
     */
    public static <T> void updataTable(Dao<T, Integer> dao, Dao<LastUpdateTime, Integer> lastUpdateTimeDao,
            List<T> lists, String tableName) {
        try {
            List<LastUpdateTime> times = lastUpdateTimeDao.queryForEq("name", tableName);
            if (times.size() == 0) {
                LastUpdateTime time = new LastUpdateTime();
                time.name = tableName;
                time.record = DateUtils.getCurDateTime();
                lastUpdateTimeDao.create(time);
            } else {
                UpdateBuilder<LastUpdateTime, Integer> updateBuilder = lastUpdateTimeDao.updateBuilder();
                updateBuilder.updateColumnValue("record", DateUtils.getCurDateTime());
                updateBuilder.where().eq("name", tableName);
                updateBuilder.update();
            }

            dao.delete(dao.queryForAll());
            for (int a = 0; a < lists.size(); a++) {
                dao.create(lists.get(a));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /***
     * 判断数据库数据是否有效，当为空、过期时无效；无网络时过期数据有效
     */
    public static boolean isDataValid(Dao<LastUpdateTime, Integer> lastUpdateTimeDao, String tableName, long timeDiffer) {
        List<LastUpdateTime> times;
        try {
            times = lastUpdateTimeDao.queryForEq("name", tableName);
            if (times.size() == 0) {
                return false;
            } else {

                // 行业数据过期，重新加载数据
                long differ = DateUtils.getMinutesBetween(DateUtils.getCurDateTime(), times.get(0).record);
                if (differ < timeDiffer) {
                    return true;
                } else {
                    if (!NetWorkUtils.isNetConnected()) {
                        return true;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false; //
    }
}
